Titanium.UI.setBackgroundColor('#000');
var window= Ti.UI.createWindow();



var longitudeLabel = Ti.UI.createLabel({
	top:10,
	left:10,
	text:'No text'
});

var latitudeLabel = Ti.UI.createLabel({
	top:40,
	left:10,
	text:'No text'
});

var speedLabel = Ti.UI.createLabel({
	top:70,
	left:10,
	text:'No text'
});

var adresseLabel = Ti.UI.createLabel({
	top:100,
	left:10,
	text:'No text'
});

if (Titanium.Geolocation.locationServicesEnabled === false)
{
	Titanium.UI.createAlertDialog({title:'My app', message:'Your device has geo turned off - turn it on.'}).show();
}
else
{
	Ti.Geolocation.preferredProvider = "gps";
	Titanium.Geolocation.distanceFilter = 10;
	Titanium.Geolocation.getCurrentPosition( function(e) {
			if (!e.success) {
				alert('Could not retrieve location');
				return;
			}
			var longitude = e.coords.longitude;
			var latitude = e.coords.latitude;
			
			longitudeLabel.text = longitude;
			latitudeLabel.text = latitude;
	});
	
	
	Ti.Geolocation.addEventListener('location', function(e) {
	  if (e.error) {
	  	latitudeLabel = "error";
	    Ti.API.error('geo - position' + e.error);
	    return;
	  }
	  var latitude = e.coords.latitude;
	  var longitude = e.coords.longitude;
	  var altitude = e.coords.altitude;
	  var accuracy = e.coords.accuracy;
	  var altitudeAccuracy = e.coords.altitudeAccuracy;
	  var heading = e.coords.heading;
	  var speed = e.coords.speed;
	  var timestamp = e.coords.timestamp;
	  Ti.API.info('geo - position');
	  Ti.API.info(' - latitude: ' + latitude);
	  Ti.API.info(' - longitude: ' + longitude);
	  Ti.API.info(' - altitude: ' + altitude);
	  Ti.API.info(' - accuracy: ' + accuracy);
	  Ti.API.info(' - altitudeAccuracy: ' + altitudeAccuracy);
	  Ti.API.info(' - heading: ' + heading);
	  Ti.API.info(' - speed: ' + speed);
	  Ti.API.info(' - timestamp: ' + timestamp);
	  
	  longitudeLabel.text = longitude;
	  latitudeLabel.text = latitude;
	  speedLabel.text = speed;
	  
	  Titanium.Geolocation.reverseGeocoder(latitude,longitude,function(evt)
		{
			if (evt.success) {
				var places = evt.places;
				if (places && places.length) {
					adresseLabel.text = places[0].address;
				} else {
					adresseLabel.text = "No address found";
				}
				Ti.API.debug("reverse geolocation result = "+JSON.stringify(evt));
			}
			else {
				Ti.UI.createAlertDialog({
					title:'Reverse geo error',
					message:evt.error
				}).show();
				Ti.API.info("Code translation: "+translateErrorCode(e.code));
			}
		});
	});
}

window.add(longitudeLabel);
window.add(latitudeLabel);
window.add(speedLabel);
window.add(adresseLabel);

window.open();
if (Titanium.Platform.name == 'android')
{
	//  as the destroy handler will remove the listener, only set the pause handler to remove if you need battery savings
	Ti.Android.currentActivity.addEventListener('pause', function(e) {
		Ti.API.info("pause event received");
		if (headingAdded) {
			Ti.API.info("removing heading callback on pause");
			Titanium.Geolocation.removeEventListener('heading', headingCallback);
			headingAdded = false;
		}
		if (locationAdded) {
			Ti.API.info("removing location callback on pause");
			Titanium.Geolocation.removeEventListener('location', locationCallback);
			locationAdded = false;
		}
	});
	Ti.Android.currentActivity.addEventListener('destroy', function(e) {
		Ti.API.info("destroy event received");
		if (headingAdded) {
			Ti.API.info("removing heading callback on destroy");
			Titanium.Geolocation.removeEventListener('heading', headingCallback);
			headingAdded = false;
		}
		if (locationAdded) {
			Ti.API.info("removing location callback on destroy");
			Titanium.Geolocation.removeEventListener('location', locationCallback);
			locationAdded = false;
		}
	});
	Ti.Android.currentActivity.addEventListener('resume', function(e) {
		Ti.API.info("resume event received");
		if (!headingAdded) {
			Ti.API.info("adding heading callback on resume");
			Titanium.Geolocation.addEventListener('heading', headingCallback);
			headingAdded = true;
		}
		if (!locationAdded) {
			Ti.API.info("adding location callback on resume");
			Titanium.Geolocation.addEventListener('location', locationCallback);
			locationAdded = true;
		}
	});
}